<template>
  <view class="goods-detail-container" v-if="goods_detail">
    <!-- 轮播图 -->
    <swiper indicator-dots autoplay circular>
      <swiper-item v-for="item in goods_detail.pics" :key="item.pics_id">
        <image
          @click="preview(item.pics_big_url)"
          :src="item.pics_big_url"
          mode="scaleToFill"
        />
      </swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="price">￥{{ goods_detail.goods_price }}</view>
      <!-- 信息主体区域 -->
      <view class="goods-info-body">
        <!-- 商品名称 -->
        <view class="goods-name">{{ goods_detail.goods_name }}</view>
        <!-- 收藏 -->
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="yf">快递：免运费</view>
    </view>
    <!-- 商品详情 -->
    <!-- <view v-html="goods_detail.goods_introduce"></view> -->
    <rich-text :nodes="goods_detail.goods_introduce"></rich-text>
    <view class="goods_nav">
      <uni-goods-nav
        :fill="true"
        :options="options"
        :buttonGroup="buttonGroup"
        @click="onClick"
        @buttonClick="buttonClick"
      />
    </view>
  </view>
</template>

<script>
import { mapMutations } from 'vuex'
export default {
  data () {
    return {
      goods_detail: null,
      options: [
        {
          icon: 'shop',
          text: '店铺'
        },
        {
          icon: 'cart',
          text: '购物车',
          info: 2
        }
      ],
      buttonGroup: [
        {
          text: '加入购物车',
          backgroundColor: '#ff0000',
          color: '#fff'
        },
        {
          text: '立即购买',
          backgroundColor: '#ffa200',
          color: '#fff'
        }
      ]
    }
  },
  onLoad (options) {
    this.getGoodsDetailData(options.goods_id)
  },
  onUnload () {
    console.log('---onUnload---')
  },
  methods: {
    ...mapMutations('cart', ['addToCart']),
    async getGoodsDetailData (goods_id) {
      const res = await uni.$request({
        url: 'goods/detail',
        data: {
          goods_id
        }
      })

      if (res.meta.status === 200) {
        // 使用字符串的 replace() 方法，为 img 标签添加行内的 style 样式，从而解决图片底部空白间隙的问题
        // 使用字符串的 replace() 方法，将 webp 的后缀名替换为 jpg 的后缀名
        res.message.goods_introduce = res.message.goods_introduce
          .replace(/<img /g, '<img style="display:block;" ')
          .replace(/webp/g, 'jpg')
        this.goods_detail = res.message
      }
    },
    // 预览
    preview (current) {
      const urls = this.goods_detail.pics.map(item => item.pics_big_url)
      uni.previewImage({
        current,
        urls
      })
    },
    onClick (e) {
      if (e.content.text === '购物车') {
        uni.switchTab({ url: '/pages/cart/index' })
      }
    },
    buttonClick (e) {
      if (e.content.text === '加入购物车') {
        // 把要存储的商品对象准备好
        const goods = {
          goods_id: this.goods_detail.goods_id,
          goods_name: this.goods_detail.goods_name,
          goods_small_logo: this.goods_detail.goods_small_logo,
          goods_price: this.goods_detail.goods_price,
          goods_number: 1,
          goods_state: true
        }

        // this.$store.commit('cart/addToCart', goods)
        this.addToCart(goods)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.goods-detail-container {
  // 给页面外层的容器，添加 50px 的内padding，
  // 防止页面内容被底部的商品导航组件遮盖
  padding-bottom: 50px;
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }
  // 商品信息区域的样式
  .goods-info-box {
    padding: 10px;
    padding-right: 0;

    .price {
      color: #c00000;
      font-size: 18px;
      margin: 10px 0;
    }

    .goods-info-body {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
        padding-right: 10px;
      }
      // 收藏区域
      .favi {
        width: 120px;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1px solid #efefef;
        color: gray;
      }
    }

    // 运费
    .yf {
      margin: 10px 0;
      font-size: 12px;
      color: gray;
    }
  }
  .goods_nav {
    // 为商品导航组件添加固定定位
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
}
</style>
